import { reqPostLoginUser, reqGetquitLogin } from '@/api';
const state = {
  user: {
    name: localStorage.getItem('name'),
    token: localStorage.getItem('token')
  }
};
const actions = {
  // 登录请求
  async getUserInfoActions(context, user) {
    const result = await reqPostLoginUser(user);
    if (result.code === 200) {
      context.commit('SAVE_USER_INFO', result.data);
      return true;
    } else {
      alert(result.message);
    }
  },

  // 退出登录
  async getquitUserActions(context) {
    const result = await reqGetquitLogin();
    if (result.code === 200) {
      // 清空vuex中的数据
      context.commit('REMOVE_USER_INFO')

      localStorage.removeItem('name')
      localStorage.removeItem('token')
      return true
    } else {
      console.log(result.message);
    }
  }
};
const mutations = {
  // 存储用户登录信息
  SAVE_USER_INFO(state, payload) {
    state.user.name = payload.name;
    state.user.token = payload.token;

    localStorage.setItem('name', payload.name);
    localStorage.setItem('token', payload.token);
  },

  REMOVE_USER_INFO(state) {
    state.user.name = '';
    state.user.token = '';
  }
};
const getters = {};
export default {
  namespaced: true,
  state,
  actions,
  mutations,
  getters
};
